package com.scnb.Singleton;

public class Mgr06 {
    //双重检查方式，必须加volatile修复INSTANCE，防止编译器优化，指令重排
    private static volatile Mgr06 INSTANCE;

    private Mgr06(){}

    public static Mgr06 getINSTANCE(){
        //双重检查
        if (INSTANCE == null){
            synchronized(Mgr06.class){
                if (INSTANCE == null){
                    try{
                        Thread.sleep(20);
                    }catch(InterruptedException e){
                        e.printStackTrace();
                    }
                    INSTANCE = new Mgr06();
                }
            }
        }
        return INSTANCE;
    }

    public void m(){
        System.out.println("m");
    }

    public static void main(String[] args){
        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                System.out.println(Mgr06.getINSTANCE().hashCode());
            }).start();
        }
    }

}
